package com.itheima.web.request;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //String getMethod();   //获取请求方式：GET
        String method = req.getMethod();
        System.out.println(method);
        //String getContextPath();  //获取虚拟目录(项目访问路径) /request-demo
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        //StringBuffer getRequestURL();   //获取URl（统一资源定位符号） http://localhost:8080/request-demo/req1
        StringBuffer requestURL = req.getRequestURL();  //StringBuffer有利于线程安全
        System.out.println(requestURL.toString());
        //String getRequestURI();   //获取URI(统一资源标识符）   /request-demo/req1
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);
        //String getQueryString();  //获取请求参数 usernamezhangsan&password=123
        String queryString = req.getQueryString();
        System.out.println(queryString);

        //------------------------
        //获取请求头 String getHeader(); //user-agent:获取浏览器版本信息
        String agent = req.getHeader("user-agent");
        System.out.println(agent);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取POST 请求体：请求参数

        //1.获取字符输入流
        BufferedReader reader = req.getReader();
        //2.读取数据
        String line = reader.readLine();
        System.out.println(line);
    }
}
/**
 * Request获取请求方式的区别：
 * get方式获取参数是通过QueryString（）获取的
 * post方式获取请求参数是通过对应的输入流来获取的
 */